﻿import urllib.request, json

from Weather import Observation
from string import Template
from datetime import datetime

class ServiceGateway:
	def __init__(self, service):
		self.dateFormat = '%a, %d %b %Y %H:%M:%S %z'
		self.webService = service
		
	def getCurrentConditions(self, zip):
		template = Template('http://api.wunderground.com/api/7f808b4670cf9775/conditions/q/$location.json')
		currentObservation = self.__getCurrentObservation(template.substitute(location = zip))
		observationDate = datetime.strptime(currentObservation.observation_time_rfc822, self.dateFormat)
		return Observation.new(observationDate,currentObservation.temp_f, currentObservation.wind_mph, currentObservation.wind_degrees)
	
	def __getCurrentObservation(self, url):
		data = self.webService.getData(url)
		return ResponseObject(data).current_observation


class ResponseObject:
	def __init__(self, data):
		for key, value in data.items():
			if isinstance(value, (list, tuple)):
				setattr(self, key, [ResponseObject(x) if isinstance(x, dict) else x for x in value])
			else:
				setattr(self, key, ResponseObject(value) if isinstance(value, dict) else value)

class WebService:
	def getData(self, url):
		return json.loads(urllib.request.urlopen(url).read().decode('utf8'))

class WebServiceStub:
	def getData(self, url=''):
		return {'current_observation': {'heat_index_c': 'NA', 'local_tz_long': 'America/Denver', 'observation_location': {'city': 'Burlington', 'full': 'Burlington, Colorado', 'elevation': '4219 ft', 'country': 'US', 'longitude': '-102.29000092', 'state': 'Colorado', 'country_iso3166': 'US', 'latitude': '39.24000168'}, 'weather': 'Clear', 'local_time_rfc822': 'Fri, 06 Jan 2012 09:03:00 -0700', 'forecast_url': 'http://www.wunderground.com/US/CO/Stratton.html', 'windchill_c': '-1', 'estimated': {}, 'windchill_f': '30', 'pressure_in': '30.01', 'dewpoint_string': '21 F (-6 C)', 'solarradiation': '', 'ob_url': 'http://www.wunderground.com/cgi-bin/findweather/getForecast?query=39.24000168,-102.29000092', 'local_epoch': '1325865780', 'icon_url': 'http://icons-ak.wxug.com/i/c/k/clear.gif', 'display_location': {'city': 'Stratton', 'full': 'Stratton, CO', 'elevation': '1336.00000000', 'state_name': 'Colorado', 'zip': '80836', 'country': 'US', 'longitude': '-102.58029175', 'state': 'CO', 'country_iso3166': 'US', 'latitude': '39.30968094'}, 'precip_today_string': '0.00 in (0.0 mm)', 'dewpoint_f': 21, 'dewpoint_c': -6, 'precip_today_metric': '0.0', 'image': {'url': 'http://icons-ak.wxug.com/graphics/wu2/logo_130x80.png', 'link': 'http://www.wunderground.com', 'title': 'Weather Underground'}, 'wind_mph': 18, 'local_tz_short': 'MST', 'precip_today_in': '0.00', 'heat_index_f': 'NA', 'temp_f': 39, 'station_id': 'KITR', 'windchill_string': '30 F (-1 C)', 'temp_c': 4, 'visibility_km': '16.1', 'pressure_trend': '+', 'visibility_mi': '10.0', 'wind_string': 'From the NNE at 18 MPH Gusting to 24 MPH', 'pressure_mb': '1016', 'temperature_string': '39 F (4 C)', 'wind_dir': 'NNE', 'icon': 'clear', 'wind_degrees': 20, 'precip_1hr_in': '-9999.00', 'wind_gust_mph': '24', 'observation_time': 'Last Updated on January 6, 8:53 AM MST', 'UV': '1', 'heat_index_string': 'NA', 'observation_epoch': '1325865180', 'precip_1hr_metric': '-9999.00', 'relative_humidity': '49%', 'observation_time_rfc822': 'Mon, 09 Jan 2012 08:53:00 -0700', 'precip_1hr_string': '-9999.00 in (-9999.00 mm)', 'history_url': 'http://www.wunderground.com/history/airport/KITR/2012/1/6/DailyHistory.html'}, 'response': {'termsofService': 'http://www.wunderground.com/weather/api/d/terms.html', 'version': '0.1', 'features': {'conditions': 1}}}


